package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.dto.AlertDataReq;
import com.zzyl.nursing.dto.ProcessAlertReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-05
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }
    /**
     * 处理报警数据
     *
     * @param processReq 处理请求
     **/
    @Override
    public void processAlertData(ProcessAlertReq processReq) {
        //根据id查询报警数据
        AlertData alertData= getById(processReq.getId());
        if(ObjectUtil.isEmpty(alertData)){
            throw new RuntimeException("未找到对应的报警数据");
        }
        // 处理时间必须晚于当前时间,可以是今天
        LocalDateTime processingTime = processReq.getProcessingTime();
        if (processingTime.isBefore(LocalDateTime.now().minusDays(1))) {
            throw new BaseException("处理时间必须晚于当前时间");
        }
        alertData.setProcessingResult(processReq.getProcesssingResult());
        alertData.setProcessorId(SecurityUtils.getUserId());
        alertData.setProcessingTime(processingTime);
        alertData.setStatus(1);

    }

    /**
     * 查询报警数据列表
     * 
     * @param alertDataReq 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertDataReq alertDataReq)
    {
        return alertDataMapper.selectAlertDataList(alertDataReq);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

}
